package com.cloudsigns.factorymanager2.produce.adapter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.activity.AvatarImageShowerActivity;
import com.cloudsigns.factorymanager2.produce.bean.Produce_ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class Produce_ProductInfoAdapter extends BaseAdapter  {
    private Context context;
    private List<Produce_ProductBean> datas = new ArrayList<Produce_ProductBean>();
    private LayoutInflater inflater;
    private String TAG = "";

    private ItemRemoveListener itemRemoveListener;


    public Produce_ProductInfoAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;
    }

    public void setData(List<Produce_ProductBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }
    }

    private String selectedPid;

    public void selectProductId(String pid) {
        selectedPid = pid;
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Produce_ProductBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_produce_product_information, null);
            holder.tv_up = convertView.findViewById(R.id.tv_up);
            holder.tv_down = convertView.findViewById(R.id.tv_down);
            holder.img_right = convertView.findViewById(R.id.img_right);
            holder.img_left = convertView.findViewById(R.id.img_left);
            holder.img_selected = convertView.findViewById(R.id.img_selected);

            if (TAG.equals("ProductionTaskOrderActivity1")) {
                holder.img_left.setVisibility(View.VISIBLE);
                holder.img_right.setVisibility(View.VISIBLE);
            } else if (TAG.equals("ProduceOrderDetailActivity") || TAG.equals("ProduceReceiveOrderItemDetailActivity")) {
                holder.img_left.setVisibility(View.VISIBLE);
                holder.img_right.setVisibility(View.GONE);
            } else if (TAG.equals("ProduceEditOrderActivity")) {
                holder.img_left.setVisibility(View.VISIBLE);
                holder.img_right.setVisibility(View.GONE);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Produce_ProductBean bean = datas.get(position);
        if (bean != null) {
            holder.tv_up.setText(bean.getProductName() + "/" + bean.getAmount() + bean.getUnit());

            if (bean.getProductId() != null && bean.getProductId().equals(selectedPid)) {
                holder.img_selected.setVisibility(View.VISIBLE);
            } else {
                holder.img_selected.setVisibility(View.GONE);
            }

            String picu = bean.getPicUrl() + "";
            final String str = picu.replace("[", "").replace("]", "");

            if (null != bean.getPicUrl()) {
                Glide.with(context).load(new File(str)).error(R.mipmap.icon_no_photo).into(holder.img_left);

                holder.img_left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, AvatarImageShowerActivity.class);
                        intent.putExtra("imageUrl", str);
                        context.startActivity(intent);
                    }
                });
            }

            CommonEnum.ProductShapeType shape = bean.getShapeType().getType();
            if (null != shape) {
                if (shape == CommonEnum.ProductShapeType.ROUND) {
                    holder.tv_down.setText("直径" + bean.getShapeType().getDiameter() + "mm*" + "厚度" + bean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                    holder.tv_down.setText("长度" + bean.getShapeType().getWidth() + "mm*" + "高度" + bean.getShapeType().getHeight() + "mm*" + "厚度" + bean.getShapeType().getThick() + "mm");
                } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                    holder.tv_down.setText(bean.getShapeType().getDesc());
                }
            }
        }

        holder.img_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemRemoveListener != null) {
                    itemRemoveListener.remove(position);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        private TextView tv_up;
        private TextView tv_down;
        private ImageView img_right;
        private ImageView img_left;
        private ImageView img_selected;
    }

}
